﻿
CREATE PROCEDURE [dbo].[ActSeriiNumerice] 
	-- Add the parameters for the stored procedure here
	@IDSerie varchar(20),@CodUnitate smallint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	DECLARE @UltNr varchar(20);
	DECLARE @StartNr varchar(20);
	DECLARE @DecUltNr decimal(18,0)
	DECLARE @DecStartNr decimal(18,0)
	DECLARE @Prefix varchar(20)
	SET @UltNr = '1'
	SELECT @StartNr = NrStart,@UltNr = UltimulNrFolosit FROM [SeriiNumerice] WHERE [CodUnitate]=@CodUnitate AND [IdSerie]=@IdSerie		

	IF @StartNr is null
		RETURN
	SET @DecUltNr = CAST(@UltNr AS DECIMAL(18,0))
	SET @Prefix = LTRIM(RTRIM(SUBSTRING(@UltNr,1, LEN(@UltNr)-LEN(RTRIM(LTRIM(CAST(@DecUltNr AS VARCHAR(20))))))))
	SET @DecStartNr = CAST(@StartNr AS DECIMAL(18,0))
	

    -- Insert statements for procedure here
	UPDATE [SeriiNumerice] SET UltimulNrFolosit = @Prefix + LTRIM(RTRIM(CAST(CASE WHEN @DecUltNr>@DecStartNr THEN @DecUltNr + 1 ELSE @DecStartNr+1 END  AS VARCHAR(20)))),
		[UltimaDataFolosit]  = GETDATE() WHERE [CodUnitate]=@CodUnitate AND [IdSerie]=@IdSerie		
END